package com.wangjiacai.utils.ali.asr;

import com.alibaba.fastjson.JSON;
import com.alibaba.nls.client.protocol.asr.SpeechTranscriberResponse;
import com.alibaba.nls.client.transport.ConnectionListener;
import com.wangjiacai.utils.ali.NettyConnection;
import com.wangjiacai.utils.ali.NettySpeechListener;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.nio.ByteBuffer;

@Data
@Slf4j
public abstract class NettySpeechTranscriberListener implements NettySpeechListener {

    private NettyConnection nettyConnection;
    private ConnectionListener connectionListener;

    public void onTranscriberStart(SpeechTranscriberResponse response) {
    }

    public void onSentenceBegin(SpeechTranscriberResponse response) {
    }

    public void onSentenceEnd(SpeechTranscriberResponse response) {
    }

    public void onTranscriptionResultChange(SpeechTranscriberResponse response) {
    }

    public void onTranscriptionComplete(SpeechTranscriberResponse response) {
    }

    @Override
    public void onClose(int closeCode, String reason) {
        if (nettyConnection != null) {
            nettyConnection.markClosed();
        }
        log.info("connection is closed due to {},code:{}", reason, closeCode);
    }

    public void onFail(int status, String reason) {
        log.error("fail status:{}, reasone:{}", status, reason);
    }

    public void onOpen() {
        log.debug("connection is ok");
    }

    public void onError(Throwable throwable) {
        log.error(throwable.getMessage(), throwable);
    }

    public void onMessage(String message) {
        if (message != null && message.trim().length() != 0) {
            log.debug("on message:{}", message);
            SpeechTranscriberResponse response = JSON.parseObject(message, SpeechTranscriberResponse.class);
            if (this.isTranscriptionStarted(response)) {
                this.onTranscriberStart(response);
                nettyConnection.markTranscriberReady();
            } else if (this.isSentenceBegin(response)) {
                this.onSentenceBegin(response);
            } else if (this.isSentenceEnd(response)) {
                this.onSentenceEnd(response);
            } else if (this.isTranscriptionResultChanged(response)) {
                this.onTranscriptionResultChange(response);
            } else if (this.isTranscriptionCompleted(response)) {
                this.onTranscriptionComplete(response);
                nettyConnection.markTranscriberComplete();
            } else if (this.isTaskFailed(response)) {
                this.onFail(response.getStatus(), response.getStatusText());
                nettyConnection.markFail();
            } else {
                log.error("can not process this message: {}", message);
            }

        }
    }

    public void onMessage(ByteBuffer message) {
    }

    private boolean isTranscriptionStarted(SpeechTranscriberResponse response) {
        String name = response.getName();
        return name.equals("TranscriptionStarted");
    }

    private boolean isSentenceBegin(SpeechTranscriberResponse response) {
        String name = response.getName();
        return name.equals("SentenceBegin");
    }

    private boolean isSentenceEnd(SpeechTranscriberResponse response) {
        String name = response.getName();
        return name.equals("SentenceEnd");
    }

    private boolean isTranscriptionResultChanged(SpeechTranscriberResponse response) {
        String name = response.getName();
        return name.equals("TranscriptionResultChanged");
    }

    private boolean isTranscriptionCompleted(SpeechTranscriberResponse response) {
        String name = response.getName();
        return name.equals("TranscriptionCompleted");
    }

    private boolean isTaskFailed(SpeechTranscriberResponse response) {
        String name = response.getName();
        return name.equals("TaskFailed");
    }

}
